'''
Created on 03.06.2012

@author: johannes
'''
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.Label import Label
from pyjamas.ui.TextBox import TextBox
from pyjamas.ui.TextArea import TextArea
from pyjamas.ui.Button import Button
from pyjamas.ui.DialogBox import DialogBox
from pyjamas.ui.HTML import HTML
from BackendProxy import BackendProxy
from pyjamas import Window

class NoteApp:
    def onModuleLoad(self):
        self.remote = BackendProxy()
        vp = VerticalPanel()
        self.notes_panel = VerticalPanel()
        vp.add(HTML("<h1>Hello World</h1>"))
        vp.add(HTML("<h2>Last 10 notes</h2>"))
        vp.add(self.notes_panel)
        vp.add(HTML("<h2>add a note</h2>"))
        self.note_box=TextBox()
        vp.add(self.note_box)
        vp.add(Button("save", getattr(self,'post_note')))
        vp.add(Button("see source", getattr(self,'see_source')))

        RootPanel().add(vp)
        self.remote.get_notes(self)

    def onRemoteResponse(self, response, request_info):
        if request_info.method == 'get_notes' or request_info.method == 'post_note':
            self.notes_panel.clear()
            for item in response:
                self.notes_panel.add(Label(item['content'] + ' | ' + item['date']))
        elif request_info.method == 'get_source':
            d = DialogBox(autoHide=True, glass=True)
            d.setHTML("Source")
            d.setPopupPosition(100, 100)
            t = TextArea()
            t.setWidth(Window.getClientWidth()-200)
            t.setHeight(Window.getClientHeight()-200)
            t.setText(response[0])
            d.setWidget(t)
            d.show()


    def onRemoteError(self, code, message, request_info):
        print "Remote Error!"

    def post_note(self):
        self.remote.post_note(self, self.note_box.getText())

    def see_source(self):
        self.remote.get_source(self)

if __name__ == '__main__':
    app = NoteApp()
    app.onModuleLoad()
